import { setData, getData } from "@/utils/storage.js";
import { defineStore } from "pinia";

let useSafeStore = defineStore("SafeStore", {
  state: () => {
    return {
      password: getData("Tk") || null,
      fingerPrint: getData("fingerPrint") || false,
      usePassword: getData("usePassword") || false,
      isAuth: false,
    };
  },
  actions: {
    savePassword(password) {
      let pwd = btoa(password);
      if (setData("Tk", pwd)) this.password = pwd;
    },
    checkpassword(password) {
      if (this.password) {
        let pwd = atob(this.password);
        if (pwd == password) {
          return true;
        } else {
          return false;
        }
      } else {
        return false;
      }
    },
    saveFingerPrint(fingerPrint) {
      this.fingerPrint = fingerPrint;
      setData("fingerPrint", fingerPrint);
    },
    savePasswordPrint(usePassword) {
      this.usePassword = usePassword;
      setData("usePassword", usePassword);
    },
    changeIsAuth() {
      this.isAuth = !this.isAuth;
    },
  },
});

export default useSafeStore;
